// 云函数 initCurrency：初始化通货数据到云数据库
const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
const db = cloud.database()

// Mock 数据（与题目中 currencyData 完全一致）
const currencyData = [
  {
    "_id": "chaos_orb",
    "name": "混沌石",
    "icon_url": "/static/images/混沌石.webp",
    "description": "1. 随机移除稀有物品的一个词缀；\n2. 为稀有物品新增一个随机词缀（核心硬通货）；\n3. 用于玩家间高价值交易。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.12,
      "trend": "up",
      "change_percent": 2.5
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.12 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.11 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.115 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.12 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.11 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.115 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.12 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.105 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.108 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.11 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.112 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.11 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.115 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.12 }
      ]
    }
  },
  {
    "_id": "divine_orb",
    "name": "神圣石",
    "icon_url": "/static/images/神圣石.webp",
    "description": "1. 重置稀有物品的词缀数值（如调整装备属性的\"ROLL值\"）；\n2. 衡量稀有装备价值的基准（\"多少D\"即指其价格）。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 8.33,
      "trend": "down",
      "change_percent": -1.2
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 8.33 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 8.4 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 8.35 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 8.33 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 8.25 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 8.3 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 8.33 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 8.1 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 8.15 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 8.2 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 8.22 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 8.25 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 8.3 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 8.33 }
      ]
    }
  },
  {
    "_id": "exalted_orb",
    "name": "崇高石",
    "icon_url": "/static/images/currencyaddmodtorare.webp",
    "description": "1. 为稀有物品新增一个随机词缀（后期装备制作的\"终极材料\"）；\n2. 极度稀有，价格远超混沌石。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 25.0,
      "trend": "stable",
      "change_percent": 0.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 25.0 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 25.0 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 25.0 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 25.0 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 24.8 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 24.9 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 25.0 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 24.5 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 24.6 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 24.7 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 24.8 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 24.8 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 24.9 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 25.0 }
      ]
    }
  },
  {
    "_id": "mirror_of_kalandra",
    "name": "卡兰德魔镜",
    "icon_url": "/static/images/currencyupgradetounique.webp",
    "description": "1. 完美复制稀有/传奇装备（如复制毕业装）；\n2. 游戏中最昂贵的通货（\"镜子装\"的核心材料）。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 5000.0,
      "trend": "up",
      "change_percent": 5.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 5000.0 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 4800.0 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 4900.0 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 5000.0 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 4700.0 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 4800.0 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 5000.0 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 4500.0 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 4600.0 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 4700.0 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 4650.0 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 4700.0 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 4800.0 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 5000.0 }
      ]
    }
  },
  {
    "_id": "orb_of_transmutation",
    "name": "点金石",
    "icon_url": "/static/images/currencyupgradetorare.webp",
    "description": "1. 将白色装备升级为带有4个词缀的稀有装备；\n2. 开图必备（将普通地图转为稀有地图以提升收益）。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.02,
      "trend": "down",
      "change_percent": -5.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.02 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.021 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.019 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.02 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.022 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.021 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.02 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.025 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.024 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.023 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.022 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.022 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.021 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.02 }
      ]
    }
  },
  {
    "_id": "orb_of_corruption",
    "name": "瓦尔宝珠",
    "icon_url": "/static/images/currencyrerollsocketnumbers02.webp",
    "description": "1. 腐化装备（随机改变属性，可能获得强力词缀或报废）；\n2. 高风险高回报的\"赌博式\"强化材料。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.5,
      "trend": "up",
      "change_percent": 3.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.5 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.48 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.49 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.5 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.47 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.48 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.5 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.45 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.46 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.47 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.48 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.47 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.48 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.5 }
      ]
    }
  },
  {
    "_id": "orb_of_augmentation",
    "name": "增幅石",
    "icon_url": "/static/images/currencyaddmodtomagic.webp",
    "description": "1. 为魔法物品（蓝色）新增一个随机词缀；\n2. 前期易获取，适合提升低级装备的基础属性。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.01,
      "trend": "stable",
      "change_percent": 0.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.01 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.01 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.01 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.01 }
      ]
    }
  },
  {
    "_id": "orb_of_alchemy",
    "name": "富豪石",
    "icon_url": "/static/images/currencyupgrademagictorare.webp",
    "description": "1. 将魔法物品升级为稀有物品，并新增一个词缀；\n2. 与增幅石联动，形成\"白装→蓝装→黄装\"的制作链。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.08,
      "trend": "up",
      "change_percent": 1.5
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.08 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.079 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.081 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.08 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.078 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.079 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.08 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.075 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.076 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.077 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.078 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.078 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.079 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.08 }
      ]
    }
  },
  {
    "_id": "orb_of_alteration",
    "name": "蜕变石",
    "icon_url": "/static/images/currencyupgradetomagic.webp",
    "description": "1. 将白色装备升级为带有1个词缀的魔法物品；\n2. 与富豪石组合，是前期装备制作的\"基础配方\"。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.015,
      "trend": "down",
      "change_percent": -2.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.015 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.016 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.014 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.015 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.016 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.0155 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.015 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.018 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.017 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.0165 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.016 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.016 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.0155 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.015 }
      ]
    }
  },
  {
    "_id": "orb_of_chance",
    "name": "机会石",
    "icon_url": "/static/images/currencyupgradetounique.webp",
    "description": "1. 将白色装备随机转为稀有/传奇装备（失败率较高）；\n2. 适合追求\"欧皇\"体验的玩家（如赌\"猎首\"腰带）。",
    "created_at": "2025-09-20T08:00:00.000Z",
    "updated_at": "2025-10-10T02:00:00.000Z",
    "price": {
      "cny_rate": 0.03,
      "trend": "stable",
      "change_percent": 0.0
    },
    "history": {
      "1d": [
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-10T06:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-10T12:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-10T18:00:00.000Z", "price": 0.03 }
      ],
      "3d": [
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.03 }
      ],
      "7d": [
        { "time": "2025-10-04T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-05T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-06T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-07T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-08T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-09T00:00:00.000Z", "price": 0.03 },
        { "time": "2025-10-10T00:00:00.000Z", "price": 0.03 }
      ]
    }
  }
]

exports.main = async (event, context) => {
  try {
    // 1. 删除集合中所有现有文档（首次执行时无旧数据，可直接插入）
    const deleteRes = await db.collection('currency').where({}).remove()
    console.log('旧数据已清空', deleteRes)

    // 2. 批量插入 Mock 数据
    const insertPromises = currencyData.map(item => {
      return db.collection('currency').add({
        data: item
      })
    })
    
    const insertRes = await Promise.all(insertPromises)
    console.log('新数据插入成功，插入数量：', insertRes.length)

    return {
      success: true,
      message: '通货数据初始化成功',
      data: {
        insertedCount: insertRes.length
      }
    }
  } catch (error) {
    console.error('初始化失败：', error)
    return {
      success: false,
      message: '初始化失败',
      error: error.message
    }
  }
}